package com.example.timetable;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;

import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;

import com.alibaba.android.arouter.facade.annotation.Route;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;
import butterknife.OnItemLongClick;
import butterknife.OnItemSelected;

@Route(path="/TimeTable/MyScore")
public class MyScore extends AppCompatActivity implements Dialog.OnDismissListener {

    @BindView(R.id.spinner_score)
    Spinner spinnerScore;
    @BindView(R.id.list_view_score)
    ListView listView;
    SharedPreferences preferences;
    SharedPreferences.Editor editor;
    String [] spinnerItems={"大一上","大一下","大二上","大二下","大三上","大三下","大四上","大四下"};
    Map<String,String> map;
    ArrayList<Map<String,Object>> arrayList;
    SimpleAdapter simpleAdapter;
    String selectTerm="大一上";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_score);
        ButterKnife.bind(this);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        ArrayAdapter<String> arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,spinnerItems);
        spinnerScore.setAdapter(arrayAdapter);
    }
    protected void showListView(){
        String fileName="MyScore"+selectTerm;
        preferences = getSharedPreferences(fileName, MODE_PRIVATE);
        editor=preferences.edit();
        arrayList=new ArrayList<>();
        //获取数据
        map = (Map<String, String>) preferences.getAll();
        Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map<String, Object> data = new HashMap<>();
            Map.Entry<String, String> entry = it.next();
            data.put("text1", entry.getKey());
            data.put("text2", entry.getValue());
            arrayList.add(data);
        }
        simpleAdapter = new SimpleAdapter(this, arrayList, R.layout.list_view_item1, new String[]{"text1", "text2"}, new int[]{R.id.text1, R.id.text2});
        listView.setAdapter(simpleAdapter);
    }
    @OnClick(R.id.add_score)
    public void onClickAddScore(){
        String fileName="MyScore"+selectTerm;
        MyAlertDialog myAlertDialog=new MyAlertDialog(this,fileName);
        myAlertDialog.setDialog("新建成绩","课程名称","课程分数");
        myAlertDialog.show();
        myAlertDialog.setOnDismissListener(this);
    }
    @OnItemSelected(R.id.spinner_score)
    public void onItemSelectedSPscore(int position){
        selectTerm=spinnerItems[position];
        showListView();
    }
    @OnItemClick(R.id.list_view_score)
    public void onItemClickScore(int position){
        String filename="MyScore"+selectTerm;
        MyAlertDialog myAlertDialog=new MyAlertDialog(this,filename);
        myAlertDialog.setDialog("修改分数","课程名称","课程分数");
        Map<String,Object> item=arrayList.get(position);
        myAlertDialog.deleteItem((String)item.get("text1"));
        myAlertDialog.show();
        myAlertDialog.setOnDismissListener(this);
    }
    @OnItemLongClick(R.id.list_view_score)
    public void onItemLongClickGoal(int position){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        AlertDialog alertDialog=builder.create();
        builder.setTitle("是否删除？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                deleteItem(position);
                showListView();
            }
        });
        builder.setNegativeButton("返回",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                alertDialog.dismiss();
            }
        });
        builder.show();
    }

    protected void deleteItem(int position){
        Map<String,Object> item=arrayList.get(position);
        editor.remove((String)item.get("text1"));
        editor.commit();
    }
    @Override
    public void onDismiss(DialogInterface dialog) {
        showListView();
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
